package com.wuyan.locationdog.ui.msg

import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.ext.createEmptyView
import com.blankj.utilcode.util.ActivityUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.MsgListAdapter
import com.wuyan.locationdog.databinding.MsgFragmentBinding
import com.wuyan.locationdog.help.MsgHelp
import com.wuyan.locationdog.help.finishRefreshAndLoadMoreNoMoreData
import com.wuyan.locationdog.viewmodel.MsgViewModel


class MsgActivity : BaseBindingActivity<MsgFragmentBinding>() {

    private val mViewModel: MsgViewModel by viewModels()

    private val mAdapter by lazy { MsgListAdapter(R.layout.item_msg_list, null) }

    override fun initData() {

        binding.apply {
            setTitleText("消息列表")


            recyclerView.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapter.apply {
                    setEmptyView(createEmptyView("暂无消息", com.arcsoft.commonbase.R.mipmap.empty_msg))

                    setOnItemClickListener { adapter, view, position ->
                        MsgHelp.instantMsgBeanContent = data[position]
                        ActivityUtils.startActivity(MsgDetailActivity::class.java)
                    }
                }
            }


            btnReadAll.setOnClickListener {

                mViewModel.msgReadAll(hashMapOf())
            }
            setOnRefreshLoadMore(smartRefreshLayout)
        }


    }

    override fun loadNetData() {
        super.loadNetData()
        val map = HashMap<String, Any>()
        map["pageSize"] = 10
        map["pageNum"] = currentPage
        mViewModel.msgList(map)

        mViewModel.msgUnreadCount(hashMapOf())
    }


    override fun observe() {
        super.observe()

        mViewModel.msgBean.observe(this) {
            finishRefreshAndLoadMoreNoMoreData(binding.smartRefreshLayout, currentPage, it.total)
            if (currentPage == 1) {
                mAdapter.setNewInstance(it.content.toMutableList())
            } else {
                mAdapter.addData(it.content.toMutableList())
            }
        }
        mViewModel.msgUnReadCountBean.observe(this) {

        }
        mViewModel.anyBean.observe(this) {
            currentPage = 1
            loadNetData()
        }
    }

    override fun onResume() {
        super.onResume()
        loadNetData()

    }
}